import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qunaimedical/components/custom_avatar.dart';
import 'package:qunaimedical/init/provider.dart';
import 'package:qunaimedical/models/comment.dart';

/// 评论
class ItemComment extends StatelessWidget {
  final Comment item;
  final GestureTapCallback onDelete;
  final GestureTapCallback onReply;

  const ItemComment(this.item, {Key key, this.onDelete, this.onReply})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<PopupMenuItem<int>> mores = [
      PopupMenuItem(value: 0, child: Text('回复')),
    ];
    if (context.read<StoreInfo>().user != null &&
        context.read<StoreInfo>().user.id == item.repliedId) {
      mores.add(PopupMenuItem(value: 1, child: Text('删除')));
    }
    return Container(
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(color: Colors.grey[200])),
        color: Colors.white,
      ),
      padding: EdgeInsets.all(16),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          CustomAvatar(
            avatarUrl:
                item.byRepliedId == null ? item.avatar : item.repliedAvatar,
            size: 30,
            margin: EdgeInsets.only(left: item.byRepliedId == null ? 0 : 40),
          ),
          SizedBox(width: 16),
          Expanded(
            child: Stack(
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Text(
                      item.repliedName,
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    Offstage(
                      offstage: item.byRepliedId == null,
                      child: Text(
                        '回复 @${item.byRepliedName}',
                        style: TextStyle(fontSize: 12),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8.0),
                      child: Text(
                        item.byRepliedId == null
                            ? item.content
                            : item.replyContent,
                        style: TextStyle(color: Colors.grey[700]),
                      ),
                    ),
                    Text(
                      item.byRepliedId == null ? item.time : item.createDate,
                      style: Theme.of(context).textTheme.caption,
                    ),
                  ],
                ),
                Positioned(
                  right: 0,
                  top: 0,
                  child: PopupMenuButton(
                    offset: Offset(0, 30),
                    icon: Icon(
                      Icons.more_horiz,
                      color: Colors.grey,
                    ),
                    onSelected: (int value) {
                      switch (value) {
                        case 0:
                          // 回复
                          onReply();
                          break;
                        case 1:
                          // 删除
                          onDelete();
                          break;
                        default:
                      }
                    },
                    itemBuilder: (BuildContext context) => mores,
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
